package com.example.john.adapterdemo;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Map;

public class MyBaseAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<Map<String,Object>> data;
        public  MyBaseAdapter(Context context,List<Map<String,Object>> data){
            this.data=data;
            inflater=LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            News news=new News();
            news.setTitle((String)data.get(position).get("title"));
            news.setContent((String)data.get(position).get("content"));
            return news;
        }
        //自行定义id与position的关系
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null)
            convertView =inflater.inflate(R.layout.news1_layout,null);
            ImageView snapshot=(ImageView)convertView.findViewById(R.id.snapshot);
            snapshot.setBackgroundResource((Integer) data.get(position).get("snapshot"));

            TextView title=(TextView)convertView.findViewById(R.id.titel);

            title.setText((String)data.get(position).get("title"));

            TextView content=(TextView)convertView.findViewById(R.id.content);
            content.setText((String)data.get(position).get("content"));
            //已经添加上了事件，这不同于SimpleAdapter
            content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("john","content.........");
                }
            });
            return convertView;
        }
    }